<template>
  <div>
    <!-- title -->
    <div class="title">
      <h2>用户列表</h2>
      <el-button type="primary" @click="add">添 加</el-button>
    </div>

    <!-- 用户列表 -->
    <el-table :data="userList" style="width: 100%">
      <el-table-column prop="id" label="序号" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="mobile" label="手机号" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column label="性别">
        <template #default="scope">
          <div style="display: flex; align-items: center; justify-content: center">
            <el-tag style="margin-left: 10px" :type="scope.row.gender == 1 ? '' : 'warning'">
              {{ scope.row.gender == 1 ? '男' : scope.row.gender == 2 ? '女' : '保密' }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          <div style="display: flex; align-items: center; justify-content: center">
            <el-tag style="margin-left: 10px" :type="scope.row.status == 1 ? '' : 'warning'">
              {{ scope.row.status == 1 ? '正常' : '禁用' }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <el-row :gutter="10">
          <el-col :span="12"><el-button type="primary">编辑</el-button></el-col>
          <el-col :span="12"><el-button type="danger">删除</el-button></el-col>
        </el-row>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      layout="prev, pager, next"
      :page-count="userListNumber"
      @update:current-page="changePage"
    />

    <!-- 添加用户 -->
    <el-dialog
      v-model="isShow"
      title="添加用户"
      align-center
      :style="{ maxWidth: '520px', minWidth: '300px' }"
      :before-close="handleClose"
    >
      <el-form :model="addInfo">
        <el-form-item>
          <el-input v-model="addInfo.username" type="text" placeholder="用户名" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="addInfo.password" type="password" placeholder="密码" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="addInfo.email" type="text" placeholder="邮箱" />
        </el-form-item>
        <el-form-item>
          <el-radio-group v-model="addInfo.gender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
            <el-radio :label="3">保密</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-input v-model="addInfo.mobile" type="number" placeholder="手机号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="isShow = false">取消</el-button>
          <el-button type="primary" @click="rsubmit(ruleFormRef)">提交</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'

// 引入接口 --- 用户列表 (含分页) / 添加用户
import { getUserList, addUser } from '@/api/userApi'

// 人员列表
const userList = ref([])

// 当前页
const currentPage = ref(1)

// 共几页
const userListNumber = ref(0)

// 获取人员列表
function getuserlist(page: number) {
  getUserList({ page }).then((res) => {
    if (res.data.errNo === 0) {
      userList.value = res.data.paginate.data
      userListNumber.value = res.data.paginate.last_page
    }
  })
}
getuserlist(currentPage.value)

// 换页
function changePage(page: number) {
  getuserlist(page)
}

// 是否显示添加页
const isShow = ref(false)

// 添加信息
const addInfo = ref({
  username: '',
  password: '',
  email: '',
  gender: 1,
  mobile: ''
})

// 打开添加管理员面板
function add() {
  isShow.value = true
}

// 提交添加信息
function rsubmit() {
  // 将手机号转成数字类型
  addInfo.value.mobile = Number(addInfo.value.mobile)

  // 添加用户
  addUser(addInfo.value).then((res) => {
    if (res.data.errNo === 0) {
      getuserlist(currentPage.value)
    }
    isShow.value = !isShow.value
    // 还原信息
    addInfo.value = {
      username: '',
      password: '',
      email: '',
      gender: 1,
      mobile: ''
    }
  })
}

// 关闭弹窗触发
function handleClose(done: () => void) {
  ElMessageBox.confirm('Are you sure to close this dialog?')
    .then(() => {
      // 还原信息
      addInfo.value = {
        username: '',
        password: '',
        email: '',
        gender: 1,
        mobile: ''
      }
      done()
    })
    .catch(() => {
      // catch error
    })
}
</script>

<style scoped>
.title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
